﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:ClearMine.Framework.Dialogs"
                    xmlns:con="clr-namespace:ClearMine.Framework.Controls"
                    xmlns:e="clr-namespace:ClearMine.Effects;assembly=ClearMine.Effects"
                    xmlns:c="clr-namespace:ClearMine.Framework.Converters">
    <c:LightUpsToBooleanConverter x:Key="LightUpsToBooleanConverter" />
    <c:ObjectToVisibilityConverter x:Key="ObjectToVisibilityConverter" />
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    <Style TargetType="{x:Type local:OptionDialog}"
           BasedOn="{StaticResource {x:Type Window}}">
        <!-- Setting the WindowStartupLocation causing an ArgumentNullException in WPF -->
        <!--<Setter Property="WindowStartupLocation"
                    Value="CenterOwner" />-->
        <Setter Property="TextOptions.TextFormattingMode"
                Value="Display" />
        <Setter Property="SizeToContent"
                Value="WidthAndHeight"/>
        <Setter Property="ShowInTaskbar"
                Value="False" />
        <Setter Property="ResizeMode"
                Value="NoResize" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:OptionDialog}">
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="showDetails">
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="separator1">
                                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1">
                                    <EasingDoubleKeyFrame.EasingFunction>
                                        <CircleEase EasingMode="EaseOut"/>
                                    </EasingDoubleKeyFrame.EasingFunction>
                                </EasingDoubleKeyFrame>
                            </DoubleAnimationUsingKeyFrames>
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.LayoutTransform).(ScaleTransform.ScaleY)"
                                                           Storyboard.TargetName="contentpanel">
                                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1" >
                                    <EasingDoubleKeyFrame.EasingFunction>
                                        <CircleEase EasingMode="EaseOut"/>
                                    </EasingDoubleKeyFrame.EasingFunction>
                                </EasingDoubleKeyFrame>
                            </DoubleAnimationUsingKeyFrames>
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
                                                           Storyboard.TargetName="contentpanel">
                                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1">
                                    <EasingDoubleKeyFrame.EasingFunction>
                                        <CubicEase EasingMode="EaseIn"/>
                                    </EasingDoubleKeyFrame.EasingFunction>
                                </EasingDoubleKeyFrame>
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                        <Storyboard x:Key="hideDetails">
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="separator1">
                                <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0">
                                    <EasingDoubleKeyFrame.EasingFunction>
                                        <CircleEase EasingMode="EaseOut"/>
                                    </EasingDoubleKeyFrame.EasingFunction>
                                </EasingDoubleKeyFrame>
                            </DoubleAnimationUsingKeyFrames>
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.LayoutTransform).(ScaleTransform.ScaleY)"
                                                           Storyboard.TargetName="contentpanel">
                                <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0">
                                    <EasingDoubleKeyFrame.EasingFunction>
                                        <CircleEase EasingMode="EaseOut"/>
                                    </EasingDoubleKeyFrame.EasingFunction>
                                </EasingDoubleKeyFrame>
                            </DoubleAnimationUsingKeyFrames>
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
                                                           Storyboard.TargetName="contentpanel">
                                <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0">
                                    <EasingDoubleKeyFrame.EasingFunction>
                                        <CubicEase EasingMode="EaseOut"/>
                                    </EasingDoubleKeyFrame.EasingFunction>
                                </EasingDoubleKeyFrame>
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                    </ControlTemplate.Resources>
                    <DockPanel Background="{TemplateBinding Background}">
                        <DockPanel DockPanel.Dock="Top">
                            <Image Source="{TemplateBinding ImageSource}"
                                   Margin="12" />
                            <TextBox Text="{TemplateBinding Message}"
                                     TextWrapping="Wrap"
                                     IsReadOnly="True"
                                     BorderThickness="0"
                                     FontSize="14pt"
                                     Margin="0,12,12,12"
                                     Foreground="#FF003399" />
                        </DockPanel>
                        <DockPanel LastChildFill="False"
                                   Background="#FFF0F0F0"
                                   DockPanel.Dock="Bottom">
                            <Expander x:Name="detailExpander"
                                      Header="{TemplateBinding ExpanderMessage}"
                                      Margin="12"
                                      Visibility="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource ObjectToVisibilityConverter}}" />
                            <ItemsControl DockPanel.Dock="Right"
                                          Margin="0,12,12,12"
                                          ItemsSource="{TemplateBinding Options}">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <VirtualizingStackPanel Orientation="Horizontal" />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                            </ItemsControl>
                        </DockPanel>
                        <Separator x:Name="separator" Margin="0"
                                   DockPanel.Dock="Bottom"
                                   Foreground="#FFDFDFDF" />
                        <Separator x:Name="separator1" Margin="0"
                                   DockPanel.Dock="Top"
                                   Opacity="0"
                                   Foreground="#FFDFDFDF" />
                        <AdornerDecorator x:Name="adornerDecorator">
                            <ContentPresenter x:Name="contentpanel" Opacity="0">
                                <ContentPresenter.LayoutTransform>
                                    <ScaleTransform ScaleY="0" />
                                </ContentPresenter.LayoutTransform>
                            </ContentPresenter>
                        </AdornerDecorator>
                    </DockPanel>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsExpanded" Value="True" SourceName="detailExpander">
                            <Trigger.ExitActions>
                                <BeginStoryboard x:Name="hideDetails_BeginStoryboard" Storyboard="{StaticResource hideDetails}"/>
                            </Trigger.ExitActions>
                            <Trigger.EnterActions>
                                <BeginStoryboard x:Name="showDetails_BeginStoryboard" Storyboard="{StaticResource showDetails}"/>
                            </Trigger.EnterActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type con:ListSortDecorator}">
        <Setter Property="HorizontalContentAlignment"
                Value="Stretch" />
        <Setter Property="VerticalContentAlignment"
                Value="Top" />
        <Setter Property="Foreground"
                Value="Gray" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type con:ListSortDecorator}">
                    <Path x:Name="path"
                          Data="M0,1L2,1L1,0Z"
                          Fill="{TemplateBinding Foreground}"
                          Stroke="{TemplateBinding Foreground}"
                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                          Width="7"
                          Height="4"
                          Stretch="Fill" />
                    <ControlTemplate.Triggers>
                        <Trigger Property="SortDirection"
                                 Value="Descending">
                            <Setter TargetName="path"
                                    Property="Data"
                                    Value="M0,0L2,0 1,1z" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!-- Don't put any game logic here. -->
    <Style TargetType="{x:Type con:MineCellControl}">
        <Setter Property="Foreground"
                Value="Transparent" />
        <Setter Property="Background"
                Value="{DynamicResource NormalCellBackground}" />
        <Setter Property="BorderBrush"
                Value="Transparent" />
        <Setter Property="Mark"
                Value="Transparent" />
        <!-- Binding BorderThickness will make the whole panel re-layout. Peformace issue. -->
        <Setter Property="BorderThickness"
                Value="0" />
        <Setter Property="RenderOptions.EdgeMode"
                Value="Aliased" />
        <Setter Property="VerticalContentAlignment"
                Value="Center" />
        <Setter Property="HorizontalContentAlignment"
                Value="Center" />
        <Setter Property="Focusable"
                Value="False" />
        <!--Keep the default template simple. -->
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type con:MineCellControl}">
                    <!-- For better performance, this template must be as simple as possible -->
                    <!-- Don't change/add any margin, padding, thickness etc property that causing layout pass. -->
                    <!-- Of course, we can render the whole control ourselves. But at the cost of customizing capability. -->
                    <Grid>
                        <Border Background="{TemplateBinding Background}" />
                        <Border Background="{TemplateBinding Foreground}" />
                        <Border Background="{TemplateBinding Mark}" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
    <DrawingBrush x:Key="DardRedTileBrush"
                  TileMode="Tile"
                  Viewport="0,0 2,2"
                  ViewportUnits="Absolute">
        <DrawingBrush.Drawing>
            <GeometryDrawing Brush="#FF800000" Geometry="M0,0 L1,0 1,1 2,1 2,2 1,2 1,1 0,1Z" />
        </DrawingBrush.Drawing>
    </DrawingBrush>
    
    <Style x:Key="NumberStrokeStyle"
           TargetType="{x:Type Path}">
        <Setter Property="Fill"
                Value="{StaticResource DardRedTileBrush}" />
        <Setter Property="Stretch"
                Value="UniformToFill" />
        <Style.Triggers>
            <DataTrigger Value="True">
                <DataTrigger.Binding>
                    <MultiBinding Converter="{StaticResource LightUpsToBooleanConverter}">
                        <Binding Path="LightUpStrokes" RelativeSource="{RelativeSource TemplatedParent}" />
                        <Binding Path="DataContext" RelativeSource="{RelativeSource Self}" Mode="OneTime" />
                    </MultiBinding>
                </DataTrigger.Binding>
                <Setter Property="Fill" Value="Red" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

    <Style TargetType="{x:Type con:NumberPresenter}">
        <Setter Property="Background"
                Value="Black" />
        <Setter Property="Focusable"
                Value="False" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type con:NumberPresenter}">
                    <Grid Background="{TemplateBinding Background}">
                        <Path DataContext="{x:Static con:LightUps.Top}"
                              Style="{StaticResource NumberStrokeStyle}"
                              Data="M-1,-1 L12,22 M1,0 L1,1 2,1 2,2 3,2 3,3 8,3 8,2 9,2 9,1 10,1 10,0Z" />
                        <Path DataContext="{x:Static con:LightUps.TopL}"
                              Style="{StaticResource NumberStrokeStyle}"
                              Data="M-1,-1 L12,22 M0,1 L1,1 1,2 2,2 2,3 3,3 3,8 2,8 2,9 1,9 1,10 0,10Z" />
                        <Path DataContext="{x:Static con:LightUps.TopR}"
                              Style="{StaticResource NumberStrokeStyle}"
                              Data="M-1,-1 L12,22 M11,1 L10,1 10,2 9,2 9,3 8,3 8,8 9,8 9,9 10,9 10,10 11,10Z" />
                        <Path DataContext="{x:Static con:LightUps.Middle}"
                              Style="{StaticResource NumberStrokeStyle}"
                              Data="M-1,-1 L12,22 M1,10 L2,10 2,9 9,9 9,10 10,10 10,11 9,11 9,12 2,12 2,11 1,11Z" />
                        <Path DataContext="{x:Static con:LightUps.BottomL}"
                              Style="{StaticResource NumberStrokeStyle}"
                              Data="M-1,-1 L12,22 M0,11 L1,11 1,12 2,12 2,13 3,13 3,18 2,18 2,19 1,19 1,20 0,20Z" />
                        <Path DataContext="{x:Static con:LightUps.BottomR}"
                              Style="{StaticResource NumberStrokeStyle}"
                              Data="M-1,-1 L12,22 M11,11 L10,11 10,12 9,12 9,13 8,13 8,18 9,18 9,19 10,19 10,20 11,20Z" />
                        <Path DataContext="{x:Static con:LightUps.Bottom}"
                              Style="{StaticResource NumberStrokeStyle}"
                              Data="M-1,-1 L12,22 M1,21 L1,20 2,20 2,19 3,19 3,18 8,18 8,19 9,19 9,20 10,20 10,21Z" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsFloatingPoint" Value="True">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type con:NumberPresenter}">
                            <Border Background="{TemplateBinding Background}">
                                <Path Data="M-1,-1 L3,22 M0,19 L0,17 2,17 2,19Z"
                                      Fill="Red"
                                      Stretch="UniformToFill" />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

    <Style TargetType="{x:Type con:ElectronicNumber}">
        <Setter Property="Focusable"
                Value="False" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type con:ElectronicNumber}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <ItemsControl Focusable="False"
                                      ItemsSource="{TemplateBinding Numbers}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <con:NumberPresenter Number="{Binding Number}"
                                                         IsFloatingPoint="{Binding IsPoint}" />
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal" />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                        </ItemsControl>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
